package com.cf.web.autoconfigure;

import com.cf.autoconfigure.CommonConfig;
import com.cf.web.controller.MvcHandlers;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

/**
 * Created by zhumingming on 2018/6/14.
 */
@Slf4j
@Configuration
@ServletComponentScan("com.cf")
@AutoConfigureAfter(CommonConfig.class)
@EnableConfigurationProperties(WebCommonProperty.class)
public class WebCommonConfig {

    @Autowired
    private WebCommonProperty webCommonProperty;

    @Bean
    public Docket createRestApi() {
        log.info("启用swagger在线文档。。。。");
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(webCommonProperty.getDocPackage()))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(webCommonProperty.getDocName())
                .description(webCommonProperty.getDocInfo())
                .version(webCommonProperty.getDocVersion())
                .build();
    }


    @Bean
    public MvcHandlers mvcHandlers() {
        log.info("启用MvcHandlers在线文档。。。。");
        return new MvcHandlers();
    }


    /**
     * 配合 @ServerEndpoint(value = "/websocket") @Component    @OnOpen     @OnClose  @OnMessage     @OnError 一起使用
     *
     * @return
     */
    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        log.info("实例化ServerEndpointExporter");
        return new ServerEndpointExporter();
    }

}
